package com.meiyu.live.ui.main.widget;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.R;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.popj.Hookup;
import com.meiyu.live.tv.audience.AudienceActivity;
import com.meiyu.live.ui.login.LoginActivity;
import com.meiyu.live.ui.main.AnchorDetailActivity;
import com.meiyu.live.util.BitmapHelp;

public class LiveItemView extends FrameLayout implements View.OnClickListener {

    private Context context;

    private TextView tv_name, tv_content, tv_progress, tv_appoint, tv_time_left;
    private ImageView iv_tv, iv_like, iv_corner_flag;
    private ProgressBar progressBar;

    private ImageLoader imageLoader;
    private DisplayImageOptions options, options_avatar;

    private Hookup bean;
    private OnLiveItemViewClickListener listener;

    public interface OnLiveItemViewClickListener {
        void onClickLike(Hookup bean);
    }

    public LiveItemView(Context context) {
        super(context);
        init(context);
    }

    public LiveItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LiveItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.adapter_live_item, this);

        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.img_load_long)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.img_load_long)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.img_load_long)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
//                .displayer(new FadeInBitmapDisplayer(200))// 图片加载好后渐入的动画时间
                .build();

        tv_name = (TextView) findViewById(R.id.tv_name);
        iv_tv = (ImageView) findViewById(R.id.iv_tv);
        iv_like = (ImageView) findViewById(R.id.iv_like);
        iv_like.setOnClickListener(this);
        iv_corner_flag = (ImageView) findViewById(R.id.iv_corner_flag);
        tv_content = (TextView) findViewById(R.id.tv_content);
        tv_progress = (TextView) findViewById(R.id.tv_progress);
        tv_appoint = (TextView) findViewById(R.id.tv_appoint);
        tv_time_left = (TextView) findViewById(R.id.tv_time_left);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        setOnClickListener(this);
    }

    public void setData(Hookup bean) {
        this.bean = bean;
        tv_name.setText(bean.getAnchor_name());
        imageLoader.displayImage(BitmapHelp.getImgByCdn(bean.getAvatar(), "m_fill", "400", "600"), iv_tv, options);
        tv_content.setText(bean.getIntroduce());
        tv_appoint.setText("已有" + bean.getBought_persons() + "人参与");
        if (bean.getOver_plus_days() > 0) {
            tv_time_left.setText("剩余时间：" + bean.getOver_plus_days() + "天");
        } else {
            tv_time_left.setText("已结束");
        }
        int progress = bean.getPercent_shares();
        progressBar.setProgress(progress > 100 ? 100 : progress);
        tv_progress.setText(progress + "%");

        iv_like.setImageResource(TextUtils.isEmpty(bean.getAf_id()) ? R.drawable.ic_like_n : R.drawable.ic_like_s);

        /*
         * 根据约播状态码获取相应的角标图片
         * 约播状态码 （2：审核中，3：认购中，4:即将播出，5：直播中，6：到期未播迟到中）
         */
        iv_corner_flag.setVisibility(VISIBLE);
        if (bean.getStatus() == 4) {
            iv_corner_flag.setImageResource(R.drawable.ic_live_corner_soon);
        } else if (bean.getStatus() == 5) {
            iv_corner_flag.setImageResource(R.drawable.ic_live_corner_living);
        } else {
            iv_corner_flag.setVisibility(GONE);
        }
    }

    @Override
    public void onClick(View view) {
        if (bean != null) {
            if (view == this) {
                switch (bean.getStatus()) {
                    case 1:
                    case 2:
                        break;
                    case 0:
                    case 3:
                    case 4:
                    case 6: {
                        AnchorDetailActivity.start(getContext(), bean);
                    }
                    break;
                    default: {
                        AudienceActivity.start(context, bean);
                    }
                    break;
                }
            } else if (view == iv_like) {
                if (!UserConfig.getInstance().isLogin()) {
                    Intent intent = new Intent(context, LoginActivity.class);
                    context.startActivity(intent);
                    return;
                }
                if (listener != null) {
                    listener.onClickLike(bean);
                }
            }
        }
    }

    public void setOnLiveItemViewClickListener(OnLiveItemViewClickListener listener) {
        this.listener = listener;
    }
}
